const app = getApp();
var util = require('../../utils/util.js');
Page({
  data: {
    disabled: false,
    columns: ['父母', '亲戚', '朋友', '其他'],
    show: false,
    realition: "",
  },

  onLoad: function (options) {
    // console.log(options.allID)
    this.setData({
      allID: options.allID,
    })
  },
  showPopup() {
    this.setData({ show: true });
  },
  onCancel() {
    this.setData({ show: false });
  },
  onConfirm(event) {
    // console.log(event)
    this.setData({
      realition: event.detail.value,
      show: false
    })
  },
  onShow: function (e) {
    // //获取时间戳
    // this.time = new Date();
    // //时间戳转换
    // var date = util.formatTime(this.time);
    // var timeTamp = Date.parse(date) / 1000;
    // this.setData({ time: timeTamp })
  },

  //form表单提交的信息
  bindSave: function (e) {
    var names = e.detail.value.names
    var phones = e.detail.value.phones
    var messages = e.detail.value.messages
    var realition = this.data.realition
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;

    //判断信息合法性
    if (names == null) {
      wx.showToast({
        title: '请输入姓名！',
        icon: 'none',
        duration: 2000,
      })
      return false
    } else if (phones == null) {
      wx.showToast({
        title: '请输入手机号！',
        icon: 'none',
        duration: 2000,
      })
      return false
    } else if (!myreg.test(phones)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'none',
        duration: 2000
      })
      return false;
    } else if (realition == '') {
      wx.showToast({
        title: '请选择关系！',
        icon: 'none',
        duration: 2000,
      })
      return false
    } else if (messages == null) {
      wx.showToast({
        title: '请输入来访事由！',
        icon: 'none',
        duration: 2000,
      })
      return false
    }

    wx.showToast({
      title: '正在授权',
      icon:'loading',
      mask:true,
      duration: 2000,
    })

    //此云函数用于查找授权用户的手机号码是否正在被授权中
    wx.cloud.callFunction({
      name: "get-pro-MessageChannel",
      data: {
        phone: phones,
      }
    }).then(res => {
      //没有被授权的情况下
      if (res.result.length == 0) {
        //获取当前时间戳
        var time = Date.parse(new Date()) / 1000;

        //此云函数用于授权，写入数据
        wx.cloud.callFunction({
          name: "student-pro",
          data: {
            names: e.detail.value.names,
            phones: phones,
            messages: messages,
            realition: realition,
            time: time,
            allID: this.data.allID
          }
        }).then(res => {
          // console.log(res)
          wx.showToast({
            title: '授权成功',
            icon: 'success',
            mask: true,
            duration: 1500,
            success: function () {
              setTimeout(function () { wx.navigateBack({})},1000)
            },
          })
        })
      } else {
        wx.showToast({
          title: '已被授权',
          icon: 'none',
          duration: 2000,
        })
      }
    })

  }
})